﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SoftballStats_BL
{
    public partial class Player
    {
#region Other Properties
        private Guid _ID;
        private string _FirstName;
        private string _LastName;
        private string _Position;
        private char _Bats;
        private char _Throws;
        private Guid _TeamID;
        private bool _IsActive;
        private short _Number;

        public Guid ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public string FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                _FirstName = value;
            }
        }

        public string LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                _LastName = value;
            }
        }

        public string Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }

        public char Bats
        {
            get
            {
                return _Bats;
            }
            set
            {
                _Bats = value;
            }
        }

        public char Throws
        {
            get
            {
                return _Throws;
            }
            set
            {
                _Throws = value;
            }
        }

        public Guid TeamID
        {
            get
            {
                return _TeamID;
            }
            set
            {
                _TeamID = value;
            }
        }

        public bool IsActive
        {
            get
            {
                return _IsActive;
            }
            set
            {
                _IsActive = value;
            }
        }

        public short Number
        {
            get
            {
                return _Number;
            }
            set
            {
                _Number = value;
            }
        }
#endregion

#region Public Methods
        public static List<Player> GetPlayersByTeamID(Guid teamID)
        {
            List<SoftballStats_BL.Player> lstPlayers = new List<SoftballStats_BL.Player>();

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetPlayersByTeamID";
                    cmd.Parameters.AddWithValue("@TeamID", teamID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            Player thisPlayer = new Player();
                            thisPlayer.ID = Guid.Parse(dr["ID"].ToString());
                            thisPlayer.FirstName = dr["FirstName"].ToString();
                            thisPlayer.LastName = dr["LastName"].ToString();
                            thisPlayer.Position = dr["Position"].ToString();
                            thisPlayer.Bats = char.Parse(dr["Bats"].ToString());
                            thisPlayer.Throws = char.Parse(dr["Throws"].ToString());
                            if (DBNull.Value != (dr["Number"]))
                            {
                                thisPlayer.Number = short.Parse(dr["Number"].ToString());
                            }
                            lstPlayers.Add(thisPlayer);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get Players", ex);
            }

            return lstPlayers;
        }

        public static Player GetPlayerByID(Guid playerID)
        {
            Player thisPlayer = new Player();
            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetPlayerByID";
                    cmd.Parameters.AddWithValue("@ID", playerID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisPlayer.ID = Guid.Parse(dr["ID"].ToString());
                            thisPlayer.FirstName = dr["FirstName"].ToString();
                            thisPlayer.LastName = dr["LastName"].ToString();
                            thisPlayer.Position = dr["Position"].ToString();
                            thisPlayer.Bats = char.Parse(dr["Bats"].ToString());
                            thisPlayer.Throws = char.Parse(dr["Throws"].ToString());
                            thisPlayer.TeamID = Guid.Parse(dr["TeamID"].ToString());
                            thisPlayer.IsActive = bool.Parse(dr["IsActive"].ToString());
                            if (DBNull.Value != (dr["Number"]))
                            {
                                thisPlayer.Number = short.Parse(dr["Number"].ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get player", ex);
            }
            return thisPlayer;
        }

        public static string GetPlayerImage(string playerName)
        {
            string imagePath = "../Images/";

            if (File.Exists("D:\\AppDev\\Projects\\SoftballStats\\SoftballStats\\Images\\Player Cards\\" + playerName + ".jpg") || File.Exists("D:\\AppDev\\Production\\SoftballStats\\Images\\Player Cards\\" + playerName + ".jpg"))
            {
                imagePath += "Player Cards/" + playerName + ".jpg";
            }
            else
            {
                imagePath += "avatar.jpg";
            }

            return imagePath;
        }
#endregion
    }
}
